/**
 * Created by luqidong on 2017/6/8.
 */
import TDbridge from '../utils/bridge'
export const addCommas = (val)=> {
    return val && val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
};
export function isEmptyObject(obj) {
    return Object.keys(obj).length === 0;
}

export function isjson(obj) {
    return typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
}

export function isphone(value) {
    var reg = /^1[0-9]{10}$/;
    return reg.test(value);
}

/**
 * 加
 * @param num1
 * @param num2
 * @returns {number}
 */
export function numAdd(num1, num2) {
    var baseNum, baseNum1, baseNum2;
    try {
        baseNum1 = num1.toString().split(".")[1].length;
    } catch (e) {
        baseNum1 = 0;
    }
    try {
        baseNum2 = num2.toString().split(".")[1].length;
    } catch (e) {
        baseNum2 = 0;
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
    return (num1 * baseNum + num2 * baseNum) / baseNum;
};
/**减法
 * @return {number}
 */
export function numSub(num1, num2) {
    var baseNum, baseNum1, baseNum2;
    var precision;// 精度
    try {
        baseNum1 = num1.toString().split(".")[1].length;
    } catch (e) {
        baseNum1 = 0;
    }
    try {
        baseNum2 = num2.toString().split(".")[1].length;
    } catch (e) {
        baseNum2 = 0;
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
    precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;
    return ((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision);
};
/**
 * 除以
 * @return {number}
 */
export function Div(a, b) {
    var n1 = 0,
        n2 = 0;
    var baseNum3, baseNum4;
    try {
        n1 += a.toString().split(".")[1].length;
    } catch (e) {
    }
    try {
        n2 += b.toString().split(".")[1].length;
    } catch (e) {
    }
    return (a * Math.pow(10, n1)) / (b * Math.pow(10, n2)) / Math.pow(10, n1 - n2);
}


/**
 * 乘以
 * @return {number}
 */
export function Mul(a, b) {
    var n1 = 0,
        n2 = 0;
    try {
        n1 += a.toString().split(".")[1].length;
    } catch (e) {
    }
    try {
        n2 += b.toString().split(".")[1].length;
    } catch (e) {
    }
    return (a * Math.pow(10, n1)) * (b * Math.pow(10, n2)) / Math.pow(10, n1 + n2);
}

export function openIfram(url) {
    // 第一部分
    var dom, doc, where, iframe = document.createElement('iframe');
    iframe.src = "javascript:false";
    iframe.title = "";
    iframe.role = "presentation";
    (iframe.frameElement || iframe).style.cssText = "width: 0; height: 0; border: 0";
    where = document.getElementsByTagName('script');
    where = where[where.length - 1];
    where.parentNode.insertBefore(iframe, where);

    // 第二部分
    try {
        doc = iframe.contentWindow.document;
    } catch (e) {
        // IE下如果主页面修改过document.domain，那么访问用js创建的匿名iframe会发生跨域问题，必须通过js伪协议修改iframe内部的domain
        dom = document.domain;
        iframe.src = "javascript:var d=document.open();d.domain='" + dom + "';void(0);";
        doc = iframe.contentWindow.document;
    }
    doc.open()._l = function () {
        var js = this.createElement("script");
        if(dom) this.domain = dom;
        js.id = "js-iframe-async";
        js.src = url;
        this.body.appendChild(js);
    };
    doc.write('<body onload="document._l();">');
    doc.close();
}
export function isiOS(u) {
    //noinspection JSDuplicatedDeclaration
    var u = u || window.navigator.userAgent;
    return !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
}

export function toDecimal(x) {
    var f = parseFloat(x);
    if(isNaN(f)) {
        return;
    }
    f = Math.round(x * 100) / 100;
    return f;
}
export function toDecimal2(x) {
    var f = parseFloat(x);
    if(isNaN(f)) {
        return false;
    }
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if(rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}

export const getUrlKey = function (name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null;
}

export function postcall(url, params, target) {
    var tempform = document.createElement("form");
    tempform.action = url;
    tempform.method = "post";
    tempform.style.display = "none";
    if(target) {
        tempform.target = target;
    }

    for (var x in params) {
        var opt = document.createElement("input");
        opt.name = x;
        opt.value = params[x];
        tempform.appendChild(opt);
    }

    var opt = document.createElement("input");
    opt.type = "submit";
    tempform.appendChild(opt);
    document.body.appendChild(tempform);
    tempform.submit();
    document.body.removeChild(tempform);
}
export function parseDom(arg) {

    var objE = document.createElement("div");
    objE.style.display = 'none';

    objE.innerHTML = arg;

    return objE;

}
export function isWeixinBrowser() {
    return /micromessenger/.test(navigator.userAgent.toLowerCase())
}
export function addEvent(obj, type, fn) {
    if(obj.attachEvent) {
        obj['e' + type + fn] = fn;
        obj[type + fn] = function () {
            obj['e' + type + fn](window.event);
        };
        obj.attachEvent('on' + type, obj[type + fn]);
    } else
        obj.addEventListener(type, fn, false);
}
export function removeEvent(obj, type, fn) {
    if(obj.detachEvent) {
        obj.detachEvent('on' + type, obj[type + fn]);
        obj[type + fn] = null;
    } else
        obj.removeEventListener(type, fn, false);
}

export function setTitle(value) {
    //原生修改title 顺便改document的title 哎 坑比
    try {
        TDbridge.appPlugin.startAppPlugin({
            'type': 14,
            'message': `${value}`
        });
    }
    catch (e) {
        document.title = value;
    }
}

export function h5tonative(title, url, fn) {
    const jumphhdata = {
        "title": `${title}`,
        "pageType": "1",
        "url": url.indexOf('http') > -1 ? url : `${location.origin}/#${url}`,
        "jumpType": "2"
    };
    try {
        TDbridge.skipApp.startSkipApp(jumphhdata);// 跳转
    }
    catch (e) {
        fn && fn();
    }
}
export function nativetonative(pageType) {
    if(pageType === 'home') {
        try {
            TDbridge.appPlugin.startAppPlugin({'type': 21});
            return;
        }
        catch (e) {

        }

    }
    let senddata = {
        pageType,
        jumpType: 1
    };

    try {
        TDbridge.skipApp.startSkipApp(senddata);
    }
    catch (e) {

    }
}

